//
//  @ Project : Adamantium Project
//  @ File Name : Map.cpp
//  @ Date : 12/3/2012
//  @ Author : 
//
//


#include "Map.h"
#include <time.h>
Map::Map() {

}

void Map::init(BeanieCore* a_core) {
	m_core = a_core;//sets core value for drawing later

	m_supernovae.initTexture( a_core, "sprites/supernovae.png", TEX_CENTER,
		V2DF(0,0), V2DF(1, 1), 0.0f);
	m_supernovae.update();
	m_supernovae.m_scl.x = 0.4296875;
	m_supernovae.m_scl.y = 0.4296875;
	m_supernovae.update();

	//create all 100 quadrants in game with galaxyMap init
	m_galaxyMap.init(a_core, &m_galaxyPosition, m_quadrantPosition);
	m_galaxyMap.m_pSupernovae = &m_supernovae;

	//set galaxy and quadrant position after galaxy map is filled
	//with enemies, stars, and supernovas to prevent same spot errors for 
	//player and enemy or star or player in supernova quadrant
	//TESTING setting spot to a specific
	srand( time(NULL));
	bool runTillPositionsSet = false;
	do{
		int a_rand = rand() % GALAXY_QUADRANT_SIZE;
		if(!m_galaxyMap.m_quadrants[a_rand]->superNova) {
			m_galaxyPosition.x = a_rand % 10;
			m_galaxyPosition.y = a_rand / 10;
			//todo later set quadrant position properly
			//m_quadrantPosition->x = 0;
			//m_quadrantPosition->y = 0;//change later
			runTillPositionsSet = true;
		}

	}while(!runTillPositionsSet);

	//then init scanner by getCurrentLRS from galaxyMap and a two dimentional point in the grid
	//remember, grid point should be 0-9 for X and Y or an error will occour
	m_scanner.init( m_galaxyMap.getCurrentLRS(m_galaxyPosition), a_core );
	m_scanner.m_pSupernovae = &m_supernovae;

	//init clickables
	m_scanner.GrabBoundingBox( m_boxScanner);
	m_boxScanner.m_core = a_core;
	m_galaxyMap.GrabBoundingBox( m_boxGalaxy);
	m_boxGalaxy.m_core = a_core;

	for(int i = 0; i < GALAXY_QUADRANT_SIZE; ++i) {
		m_galaxyMap.GrabQuadrantBoxes( m_quadrants[i], i);
		m_quadrants[i].m_core = a_core;
	}
}

void Map::draw() {
	m_scanner.draw();//draws LRmap minimap
	m_galaxyMap.draw();//draws quadrant or galaxy map with numbers
}

void Map::reInit() {
	//Clickable::
}

Map::~Map() {

}

void Map::release() {

}

void Map::update()
{

	//testing first with keyboard moving around galaxy
	//BYTE * keyboard = m_core->keyMap();
	//if(m_core->keyPress(DIK_UPARROW)){//if up arrow pressed, decrement Y axis once if not at boarder then reinit maps
	//		if(m_galaxyPosition.y > 0) {
	//			m_galaxyPosition.y -= 1;
	//			m_scanner.reInit( m_galaxyMap.getCurrentLRS(m_galaxyPosition));
	//		}

	//}
	//if(m_core->keyPress(DIK_DOWNARROW)) {
	//	if(m_galaxyPosition.y < 9) {
	//		m_galaxyPosition.y += 1;
	//		m_scanner.reInit( m_galaxyMap.getCurrentLRS(m_galaxyPosition));
	//	}
	//}

	if(m_core->keyPress(DIK_M)) {
		m_galaxyMap.m_bDrawGalaxy = !m_galaxyMap.m_bDrawGalaxy;
	}
	if( m_boxScanner.isClicked(0)) {//if mini map is clicked then switch to galaxymap
		m_galaxyMap.m_bDrawGalaxy = !m_galaxyMap.m_bDrawGalaxy;
	}

	if(/*m_boxGalaxy.isClicked(0) && */m_galaxyMap.m_bDrawGalaxy) {//if galaxymap is clicked when in galaxy view
		for(int i = 0; i < GALAXY_QUADRANT_SIZE; ++i) {//highly inneffecient. place in do {}while() loop later to break out when square is found or at end
			if( m_quadrants[i].isClicked(0) && !m_galaxyMap.m_quadrants[i]->superNova ) {
				m_galaxyPosition.x = i % 10;
				m_galaxyPosition.y = i / 10;
				m_scanner.reInit( m_galaxyMap.getCurrentLRS(m_galaxyPosition));
				m_galaxyMap.m_bDrawGalaxy = !m_galaxyMap.m_bDrawGalaxy;
			}
		}
	}
}
